package org.jeecg.modules.demo.publicplace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.jeecg.modules.demo.publicplace.entity.ZnzfPublicOpen;
import org.jeecg.modules.demo.publicplace.mapper.ZnzfPublicOpenMapper;
import org.jeecg.modules.demo.publicplace.service.IZnzfPublicOpenService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 开放时间段
 * @Author: jeecg-boot
 * @Date:   2022-04-11
 * @Version: V1.0
 */
@Service
public class ZnzfPublicOpenServiceImpl extends ServiceImpl<ZnzfPublicOpenMapper, ZnzfPublicOpen> implements IZnzfPublicOpenService {

    @Resource
    private ZnzfPublicOpenMapper znzfPublicOpenMapper;

    /**
     * 查询公共场所的预约时间段列表
     * @param publicId
     * @return
     */
    @Override
    public List<ZnzfPublicOpen> getByPublicId(String publicId) {
        LambdaQueryWrapper<ZnzfPublicOpen> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfPublicOpen::getPublicId, publicId);
        List<ZnzfPublicOpen> znzfPublicOpenList = znzfPublicOpenMapper.selectList(queryWrapper);
        return znzfPublicOpenList;
    }

    /**
     * 根据预约时间段id
     * @param publicOpenId
     * @return
     */
    @Override
    public ZnzfPublicOpen queryZnzfPublicOpenById(String publicOpenId) {
        LambdaQueryWrapper<ZnzfPublicOpen> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfPublicOpen::getId, publicOpenId);
        return znzfPublicOpenMapper.selectOne(queryWrapper);
    }

    /**
     * 清0预约人数
     * @return
     */
    @Override
    public int emptyCurrentReservationNumber() {
        List<ZnzfPublicOpen> znzfPublicOpenList = znzfPublicOpenMapper.selectList(null);
        for (ZnzfPublicOpen znzfPublicOpen : znzfPublicOpenList) {
            znzfPublicOpen.setCurrApply(0);
            if (znzfPublicOpenMapper.updateById(znzfPublicOpen) == 0) {
                return -1;
            }
        }
        return 0;
    }
}
